package cn.tedu.charging.order.dao.mapper.repository.impl;

import cn.tedu.charging.order.common.OrderStatusConstant;
import cn.tedu.charging.order.dao.mapper.OrderFailMapper;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.dao.mapper.repository.OrderRepository;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @Author: Miyako
 * @Date: 2024-10-21-10:50
 * @Description:
 *
 */
@Repository("OrderRepository")
public class OrderRepositoryImpl implements OrderRepository {
    @Autowired
    OrderSuccessMapper orderSuccessMapper;
    @Autowired
    private OrderFailMapper orderFailMapper;

    @Override
    public Integer UpdateOrderStatusPtoE(String orderNo ) {
        UpdateWrapper wrapper = new UpdateWrapper<>();
        wrapper.eq("bill_id",orderNo);
        wrapper.eq("bill_status", OrderStatusConstant.ORDER_STATUS_PROCESS);
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_EXCEPTION_END);
       return  orderSuccessMapper.update(chargingBillSuccessPO,wrapper);
    }

    @Override
    public ChargingBillSuccessPO getSuccessOrder(String orderNo) {

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id",orderNo);

        return orderSuccessMapper.selectOne(queryWrapper);
    }

    @Override
    public ChargingBillFailPO getFailOrder(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id",orderNo);
        return orderFailMapper.selectOne(queryWrapper);
    }

    @Override
    public int saveFailOrder(ChargingBillFailPO newChargingBillFailPO) {

        return orderFailMapper.insert(newChargingBillFailPO);
    }

    @Override
    public int updateDeviceInfo(String orderNo, Integer gunId) {
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setGunId(gunId);
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bill_id",orderNo);
        return orderSuccessMapper.update(chargingBillSuccessPO, updateWrapper);
    }
}
